/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <sstream>

#include "Consumer.h"
#include "Event.h"

#include <Util/Logger.h>

namespace galaxy {

    namespace mediator {

        Consumer::Consumer ()
        {
        }

        Consumer::~Consumer () {
        }

        void Consumer::handleDelayedEvents (int max_events) {
            mediator::Event * event;
            while (max_events > 0) {
                if (popEvent ((mediator::Event*&)event) == false) {
                    return; // no events on queue
                }
                if (handleQueuedEvent (*event) == false) {
                    LOG_ERROR ("an error occurred when processing a delayed event");
                }
                terminateEvent (event);
            }
       }

        bool Consumer::pushEvent (Event& event) {
            Event * e = event.clone ();
            events.push (e);
            return true;
        }

        bool Consumer::popEvent (Event*& event) {
            return events.try_pop(event);
        }

        void Consumer::flushEvents () {
            events.clear();
        }

        void Consumer::terminateEvent (Event * event) {
            if (event) {
                delete event;
            }
        }

    } // namespace mediator

} // namespace galaxy

